package ca.uwindsor.cs.deepweb.estimation.experiment.newsgroup;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import ca.uwindsor.cs.deepweb.utility.FileSizeDistributionAnalyzer;

public class DocumentSizeReaderforNewsgroup {
	private int globalID;

	protected FileSizeDistributionAnalyzer analyzer;

	/**
	 * Constructor
	 */
	public DocumentSizeReaderforNewsgroup() {
		analyzer = new FileSizeDistributionAnalyzer(1000, null);
		globalID = 1;
	}

	/**
	 * Start to create the index to store file size
	 * 
	 * @param indexdir
	 *            Where to store the index
	 * @param docdir
	 *            Where are the documents locate
	 */
	public void go( File docdir) {
		try {
			run(docdir);
			ArrayList<Integer> distribution = analyzer.getDistribution();
			StringBuffer result = new StringBuffer();
			result.append("less than or equals to 1k letter");
			result.append("\t");
			result.append(distribution.get(0).toString());
			result.append("\n");
			for (int i = 1; i < distribution.size(); i++) {
				distribution.set(i, new Integer(distribution.get(i)
						+ distribution.get(i - 1)));
				result.append("less than or equals to " + (i + 1) + "k bytes");
				result.append("\t");
				result.append(distribution.get(i));
				result.append("\n");
			}
			System.out.println(result);
			System.out.println(globalID+" files are indexed!");
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}

	}

	protected void run(File dir) throws IOException {
		File[] dataFiles = dir.listFiles();

		// Add documents to the index
		for (int i = 0; i < dataFiles.length; i++) {
			File file = dataFiles[i];
			if (file.isFile()) {
				if (file.getName().endsWith(".txt")) {
					long sizeinbyte = file.length();

					analyzer.addSize(sizeinbyte);
					// txtReader.close();
					globalID++;
				}
			} else if (file.isDirectory()) {
				run(dataFiles[i]);
			}
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DocumentSizeReaderforNewsgroup c = new DocumentSizeReaderforNewsgroup();
		if (args.length == 1) {
			c.go(new File(args[0]));
		} else {
			System.out
					.println("usage: java -jar DocumentSizeReaderforNewsgroup documentDir");
		}
	}

}
